package cn.net.xyan.kotlin


import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.JavaType
import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.type.TypeFactory
import kotlin.reflect.KClass
import java.math.BigDecimal
import java.io.IOException



/**
 * Created by zarra on 2017/3/23.
 */

data class TransferObject<T>(var identifier: String = "") {

    companion object{
        @JvmStatic
        fun <V:Any,I:TransferObject<V>> javaType(inheritCls:KClass<I>,cls:KClass<*>):JavaType{
            return TypeFactory.defaultInstance().constructParametrizedType(TransferObject::class.java,inheritCls.java,cls.java)
        }
        @JvmStatic
        fun javaType(cls:KClass<*>):JavaType{
            return TypeFactory.defaultInstance().constructParametrizedType(TransferObject::class.java,TransferObject::class.java,cls.java)
        }
    }

    constructor(vendorID: String, body: T?) : this(vendorID) {
        this.body = body
    }

    var body: T? = null

    var sign: String = ""

}

class MoneySerializer : JsonSerializer<BigDecimal>() {
    @Throws(IOException::class, JsonProcessingException::class)
    override fun serialize(value: BigDecimal, jgen: JsonGenerator, provider: SerializerProvider) {
        // put your desired money style here
        jgen.writeString(value.setScale(2, BigDecimal.ROUND_HALF_UP).toString())
    }
}